﻿using System;
using System.IO;
using System.Windows;
using System.Xml.Linq;
using System.Windows.Controls;
using System.Linq;

namespace Qwen_for_Windows.Model
{
    class Qwen_File
    {
        private static readonly string filePath = Path.Combine(Environment.CurrentDirectory, "Data", "AppSetting.xml");
        private static readonly string DataDirectory = Path.Combine(Environment.CurrentDirectory, "Data");
        private static readonly string ChatHistoryDirectory = Path.Combine(DataDirectory, "ChatHistory");

        public static void CreateXMLFile()
        {
            // 判断文件是否存在
            if (!File.Exists(filePath))
            {
                // 创建默认配置文件
                try
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    string AppSetting = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<AppSetting>
    <!-- API_Key -->
    <ApiKeys>
        <ApiKey name=""API_Key"" value=""""/>
    </ApiKeys>

    <!-- URL-->
    <Urls>
        <Url name=""Base_URL"" value=""""/>
    </Urls>
    <!-- Model-->
    <Models>        
        <Model name=""ModelName"" value=""""/>
    </Models>
</AppSetting>";
                    // 创建并写入默认内容到文件
                    using (StreamWriter writer = new StreamWriter(filePath, false))
                    {
                        writer.Write(AppSetting);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"创建默认配置文件时出错: {ex.Message}");
                }
            }
        }

        public static void CheckChatHistoryDirectory()
        {
            // 确保 Data 目录存在
            if (!Directory.Exists(DataDirectory))
            {
                Directory.CreateDirectory(DataDirectory);

            }

            // 确保 ChatHistory 目录存在
            if (!Directory.Exists(ChatHistoryDirectory))
            {
                Directory.CreateDirectory(ChatHistoryDirectory);
            }
        }
    }

}